/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-25 14:40:27
 * @LastEditors: turingchang
 * @LastEditTime: 2020-08-27 10:23:20
 */

const FileTransport = require('egg-logger').FileTransport;
const moment = require('../utils/time').moment;
 
class CoustomJsonTransport extends FileTransport {    
    constructor(options, ctx) {
        super(options);
        this.ctx = ctx;
    }
 
    log(level, args, meta) {
        //console.log(args);
        const prefixStr = this.buildFormat(level,args);
        args= [];
        args[0] =prefixStr;
        super.log(level, args, meta);
    }
 
    buildFormat(level,args) {
        var obj ={};
        var loginfo ={};
        var err ={};
        var errmsg ='';
        if(!this.ctx.app.utils.validator.isNull(args)){
            if(args.length>0){
                loginfo =args[0];
                if(args.length>1){
                    if (args[1] instanceof Error) {
                        err = args[1];
                        errmsg =err.message;
                    }
                    err =args[1];                    
                }
            }
        }
        const timeformat =moment().format('YYYY-MM-DD HH:mm:ss.SSS');
        const time =Date.now();
        const pid =`${process.pid}`;
        const path =this.ctx.path;
        const method =this.ctx.method;
        const ip = this.ctx.ip;
        const token =this.ctx.request.header.token;
        const client =this.ctx.request.header.client; //客户端类型，一般为android、ios、web
        const client_model =this.ctx.request.header.client_model; //客户端型号，如果是移动端则为手机或平板设备型号，如果是pc端或web端则为浏览器型号
        const cookie =this.ctx.request.header.cookie;        
        var params =this.ctx.query;
        if(method !='GET'){
            params =this.ctx.request.body;
        }
        const data =this.ctx.body;  
        
        const start_time =this.ctx.start_time;
        const end_time =this.ctx.end_time;
        var interval =0;
        try{
            interval =end_time-start_time;
        }catch(err){
            
        }        
        
        obj ={
            time:time,
            time_format:timeformat,
            level:level,
            request_id:this.ctx.request_id,
            pid:pid,
            ip:ip,
            path:path,
            status:this.ctx.status,
            token:token,
            client:client,
            client_model:client_model,
            cookie:cookie,
            upload:false,
            start_time:start_time,
            end_time:end_time,
            interval:interval,
            params:params,
            response:data,
            log:loginfo
        }
        
        var res ='';
        var nobj ={};
        for(var key in obj){
            if(!this.ctx.app.utils.validator.isNull(key)){                  
                if(this.ctx.app.utils.validator.isNull(obj[key])){  
                    switch(key){
                        case 'timeformat':case 'level':
                        case 'path':case 'token':
                        case 'ip':case 'cookie':
                        case 'client':case 'client_model':
                        case 'log':obj[key] ='';break;
                        case 'upload':obj[key] =false;break;
                        case 'params':case 'data':obj[key] ={};break;
                        default:obj[key] =null;break;
                    }
                    // switch(typeof obj[key]){
                    //     case Number:obj[key] =0;break;
                    //     case String:obj[key] ='';break;
                    //     case Object:case Array:obj[key] ={};break;
                    //     default:obj[key] =null;break;
                    // }
                }  
                nobj[key] =obj[key];             
            }            
        }
        res =JSON.stringify(obj);
        res +=',';
        return res;
    } 
}
 
module.exports = CoustomJsonTransport;